Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 | 'use client' import { useState, useEffect, useCallback, useRef } from 'react' import { useParams, useRouter } from 'next/navigation' import Link from 'next/link' import type { ExecutableFlowchart, FlowchartDefinition, ProblemValue, } from '@/lib/flowcharts/schema' import { loadFlowchart } from '@/lib/flowcharts/loader' import { FlowchartWalker, FlowchartProblemInput } from '@/components/flowchart' import { PageWithNav } from '@/components/PageWithNav' import { FloatingHamburgerMenu } from '@/components/FloatingHamburgerMenu' import { css } from '../../../../styled-system/css' import { vstack } from '../../../../styled-system/patterns' type PageState = | { type: 'loading' } | { type: 'error'; message: string } | { type: 'inputting'; flowchart: ExecutableFlowchart } | { type: 'walking'; flowchart: ExecutableFlowchart; problemInput: Record<string, ProblemValue> } export default function FlowchartPage() { const params = useParams() const router = useRouter() const flowchartId = params.flowchartId as string const [state, setState] = useState<PageState>({ type: 'loading' }) // Track if we've already processed sessionStorage (prevents React Strict Mode double-run issues) const processedStorageRef = useRef(false) // Load flowchart on mount, check for stored problem values useEffect(() => { async function load() { try { // Fetch flowchart from API (checks both hardcoded and database flowcharts) const response = await fetch(`/api/flowcharts/${flowchartId}`) if (!response.ok) { const errorData = await response.json().catch(() => ({})) setState({ type: 'error', message: errorData.error || `Flowchart "${flowchartId}" not found`, }) return } const data = await response.json() const { definition, mermaid } = data.flowchart as { definition: FlowchartDefinition mermaid: string } const flowchart = await loadFlowchart(definition, mermaid) // Check for stored problem values from the picker modal // Use ref to prevent React Strict Mode double-run from losing the values if (!processedStorageRef.current) { processedStorageRef.current = true const storageKey = `flowchart-problem-${flowchartId}` const storedValues = sessionStorage.getItem(storageKey) if (storedValues) { // Clear the stored values so they don't persist across refreshes sessionStorage.removeItem(storageKey) try { const problemInput = JSON.parse(storedValues) as Record<string, ProblemValue> setState({ type: 'walking', flowchart, problemInput }) return } catch { // If parsing fails, fall through to inputting console.warn('Failed to parse stored problem values') } } // Only set to inputting on first run if no stored values found setState({ type: 'inputting', flowchart }) } // On subsequent runs (React Strict Mode), don't change state - let the first run's state persist } catch (error) { console.error('Error loading flowchart:', error) setState({ type: 'error', message: 'Failed to load flowchart' }) } } load() }, [flowchartId]) // Handle problem input submission const handleProblemSubmit = useCallback( (values: Record<string, ProblemValue>) => { if (state.type !== 'inputting') return setState({ type: 'walking', flowchart: state.flowchart, problemInput: values, }) }, [state] ) // Handle restart (go back to input) const handleRestart = useCallback(() => { if (state.type !== 'walking') return setState({ type: 'inputting', flowchart: state.flowchart }) }, [state]) // Handle completion const handleComplete = useCallback(() => { // Could save results, update progress, etc. // For now, go back to picker router.push('/flowchart') }, [router]) // Handle change problem (go back to picker modal) const handleChangeProblem = useCallback(() => { router.push('/flowchart') }, [router]) // Nav slot content - Back to flowcharts link (for non-walking states) const navSlot = ( <Link href="/flowchart" className={css({ fontSize: 'sm', color: { base: 'blue.600', _dark: 'blue.400' }, textDecoration: 'none', _hover: { textDecoration: 'underline' }, })} > ← Back to flowcharts </Link> ) // Walking mode: minimal distraction-free UI with just floating hamburger if (state.type === 'walking') { return ( <> <FloatingHamburgerMenu position="top-left" onExit={handleChangeProblem} exitLabel="Exit Flowchart" /> <FlowchartWalker flowchart={state.flowchart} problemInput={state.problemInput} onComplete={handleComplete} onRestart={handleRestart} onChangeProblem={handleChangeProblem} /> </> ) } // Non-walking states: use full nav return ( <PageWithNav navSlot={navSlot}> <div className={vstack({ gap: '4', padding: '4', minHeight: '100vh' })}> {/* Main content */} <main className={css({ flex: 1, width: '100%', maxWidth: '600px', margin: '0 auto', })} > {state.type === 'loading' && ( <div className={css({ display: 'flex', alignItems: 'center', justifyContent: 'center', height: '300px', color: { base: 'gray.500', _dark: 'gray.400' }, })} > Loading... </div> )} {state.type === 'error' && ( <div className={vstack({ gap: '4', alignItems: 'center', justifyContent: 'center', height: '300px', })} > <p className={css({ color: { base: 'red.600', _dark: 'red.400' }, fontSize: 'lg', })} > {state.message} </p> <button onClick={() => router.push('/flowchart')} className={css({ paddingX: '4', paddingY: '2', borderRadius: 'md', backgroundColor: { base: 'gray.200', _dark: 'gray.700' }, color: { base: 'gray.800', _dark: 'gray.200' }, border: 'none', cursor: 'pointer', })} > Go back </button> </div> )} {state.type === 'inputting' && ( <FlowchartProblemInput schema={state.flowchart.definition.problemInput} onSubmit={handleProblemSubmit} flowchart={state.flowchart} /> )} </main> </div> </PageWithNav> ) } |